package com.linkoog.devtools.code_generator.wizard.generate.database.table.panel;

import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.portion.DbtableLeftPanelBuilder;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.portion.DbtableRightPanelBuilder;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.ui.Splitter.DividerPositionStrategy;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class DbtableInfoContextPanel {

    private JPanel myMainPanel;

    private DbtableLeftPanelBuilder leftPanelBuilder;
    private DbtableRightPanelBuilder rightPanelBuilder;

    public DbtableInfoContextPanel() {
        initContextPanel();
    }

    private void initContextPanel() {
        leftPanelBuilder = new DbtableLeftPanelBuilder();
        rightPanelBuilder = new DbtableRightPanelBuilder();

        leftPanelBuilder.setDbtableClickConsumer(tableInfo -> {
            rightPanelBuilder.resetContextInfo(tableInfo);
        });


        leftRightSplitPanel();

        //this.myMainPanel.setBorder(IdeBorderFactory.createTitledBorder("Default Variables \\& Directives", false));
        //this.myMainPanel.add(rightPanelBuilder.getMyMainPanel(), BorderLayout.CENTER);
    }

    public void resetTableInfoJTable(List<TableInfo> tableInfoList) {
        leftPanelBuilder.resetContextInfo(tableInfoList);
        rightPanelBuilder.resetContextInfo(null);
    }

    public String validate() {
        List list = leftPanelBuilder.getDbtableJTable().getTableInfoModel().getCheckedRow();
        if (list == null || list.isEmpty()) {
            return "请最少选择一个表！";
        }
        return null;
    }

    public List<TableInfo> getSelectedDBTable() {
        List<TableInfo> list = leftPanelBuilder.getDbtableJTable().getTableInfoModel().getCheckedRow();
        return list;
    }

    private void leftRightSplitPanel() {
        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.2F);
        splitter.setResizeEnabled(false);
        splitter.setDividerPositionStrategy(DividerPositionStrategy.KEEP_FIRST_SIZE);
        splitter.setFirstComponent(leftPanelBuilder.getMyMainPanel());
        splitter.setSecondComponent(rightPanelBuilder.getMyMainPanel());

        //this.myMainPanel.setBorder(IdeBorderFactory.createTitledBorder("Table Info \\& Columns Info", false));
        this.myMainPanel.add(splitter, BorderLayout.CENTER);
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }


}
